/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AgregarCarga.java
 *
 * Created on 21/04/2009, 01:03:51 AM
 */

package quantum;

import java.awt.Color;
import javax.swing.JOptionPane;

/**
 *
 * @author alumno
 */
public class AgregarCarga extends javax.swing.JDialog {

    Color color;
    int escalaX;
    int escalaY;
    int escalaMag;
    float poscX;
    float poscY;
    float magnitud;
    Carga dato;

    /** Creates new form AgregarCarga */
    public AgregarCarga(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtX = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtY = new javax.swing.JTextField();
        txtMag = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        cmbColor = new javax.swing.JComboBox();
        cmbEscalaX = new javax.swing.JComboBox();
        cmbEscalaY = new javax.swing.JComboBox();
        cmbEscalaMag = new javax.swing.JComboBox();
        btnAgre = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Agregar Carga");
        setModal(true);
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jLabel1.setText("X:");

        jLabel2.setText("Y:");

        jLabel3.setText("Magnitud:");

        jLabel4.setText("Color:");

        cmbColor.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Rojo", "Azul", "Verde" }));

        cmbEscalaX.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "x10^1 m", "metros", "x10^-1 m", "x10^-2 m", "x10^-3 m", "x10^-6 m" }));
        cmbEscalaX.setSelectedIndex(1);

        cmbEscalaY.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "x10^1 m", "metros", "x10^-1 m", "x10^-2 m", "x10^-3 m", "x10^-6 m" }));
        cmbEscalaY.setSelectedIndex(1);

        cmbEscalaMag.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "x10^1 C", "Coulombs", "x10^-1 C", "x10^-2 C", "x10^-3 C", "x10^-6 C" }));
        cmbEscalaMag.setSelectedIndex(1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(txtMag, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(txtY, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(txtX, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 4, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4)
                        .addGap(2, 2, 2)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cmbEscalaX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbEscalaY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(cmbColor, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(cmbEscalaMag, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtMag, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(cmbEscalaX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmbEscalaY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmbEscalaMag, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmbColor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnAgre.setText("Agregar");
        btnAgre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgreActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnAgre, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnAgre)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAgreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgreActionPerformed
        recogeCombos();
        if(validaLimites()){
            if(escalaX < 0){
                poscX /= (pot(escalaX *-1));
            }else{
                poscX *= (pot(escalaX));
            }
            if(escalaY < 0){
                poscY /= (pot(escalaY *-1)) * -1;
            }else{
                poscY *= (pot(escalaY)) * -1;
            }
            if(escalaMag < 0){
                magnitud /= (pot(escalaMag * -1));
            }else{
                magnitud *= (pot(escalaMag));
            }
            dato = new Carga(poscX, poscY, magnitud, color);
            Main.plan.anadeCarga(dato);
            Main.prin.actualizaTabla();
            this.dispose();
        }
}//GEN-LAST:event_btnAgreActionPerformed

    int abs(int a){
        if(a < 0)
            return -a;
        return a;
    }

    boolean validaLimites(){
        float aux;
        boolean reg = true;
        String mensaje = null;
        //Valida el eje X
        try{
            poscX = Float.parseFloat(txtX.getText());
            if(escalaX > 0){
                aux = poscX * pot(escalaX);
                if(aux > Main.plan.maxHor){//maxHor debe de estar definido en metros
                    mensaje += "La coordenada X!!: Sobrepasa el limite superior establecido.\n";
                    reg = false;
                }
                if(aux < Main.plan.minHor){//maxHor debe de estar definido en metros
                    mensaje += "La coordenada X!!: Sobrepasa el limite inferior establecido.\n";
                    reg = false;
                }
            }else{
                aux = poscX / pot(abs(escalaX));
                if(aux > Main.plan.maxHor){//maxHor debe de estar definido en metros
                    mensaje += "La coordenada X!!: Sobrepasa el limite superior establecido.\n";
                    reg = false;
                }
                if(aux < Main.plan.minHor){//maxHor debe de estar definido en metros
                    mensaje += "La coordenada X!!: Sobrepasa el limite inferior establecido.\n";
                    reg = false;
                }
            }
        }catch(Exception e){
            mensaje += "El valor del eje X es incorrecto.\n";
            reg = false;
        }
        //Valida el eje Y
        try{
            poscY = Float.parseFloat(txtY.getText());
            if(escalaX > 0){
                aux = poscY * pot(escalaY);
                if(aux > Main.plan.maxVer){//maxHor debe de estar definido en metros
                    mensaje += "La coordenada Y!!: Sobrepasa el limite superior establecido.\n";
                    reg = false;
                }
                if(aux < Main.plan.minVer){//maxHor debe de estar definido en metros
                    mensaje += "La coordenada Y!!: Sobrepasa el limite inferior establecido.\n";
                    reg = false;
                }
            }else{
                aux = poscY / pot(abs(escalaY));
                if(aux > Main.plan.maxVer){//maxHor debe de estar definido en metros
                    mensaje += "La coordenada Y!!: Sobrepasa el limite superior establecido.\n";
                    reg = false;
                }
                if(aux < Main.plan.minVer){//maxHor debe de estar definido en metros
                    mensaje += "La coordenada Y!!: Sobrepasa el limite inferior establecido.\n";
                    reg = false;
                }
            }
        }catch(Exception e){
            mensaje += "El valor del eje Y es incorrecto.\n";
            reg = false;
        }
        //Valida la magnitud de la carga
        try{
            magnitud = Float.parseFloat(txtMag.getText());
        }catch(Exception e){
            mensaje += "El valor de la carga es incorrecto.\n";
            reg = false;
        }
        if(!reg){
            JOptionPane.showMessageDialog(this, mensaje, "Alerta", JOptionPane.ERROR_MESSAGE);
        }
        return reg;
    }

    void recogeCombos(){
        switch(cmbColor.getSelectedIndex()){
            case 0: color = Color.red;
                break;
            case 1: color = Color.blue;
                break;
            case 2: color = Color.green;
                break;            
        }
        switch(cmbEscalaX.getSelectedIndex()){
            case 0: escalaX = 1;
                break;
            case 1: escalaX = 0;
                break;
            case 2: escalaX = -1;
                break;
            case 3: escalaX = -2;
                break;
            case 4: escalaX = -3;
                break;
            case 5: escalaX = -6;
                break;
        }
        switch(cmbEscalaY.getSelectedIndex()){
            case 0: escalaY = 1;
                break;
            case 1: escalaY = 0;
                break;
            case 2: escalaY = -1;
                break;
            case 3: escalaY = -2;
                break;
            case 4: escalaY = -3;
                break;
            case 5: escalaY = -6;
                break;
        }
        switch(cmbEscalaMag.getSelectedIndex()){
            case 0:
                escalaMag = 1;
                break;
            case 1:
                escalaMag = 0;
                break;
            case 2:
                escalaMag = -1;
                break;
            case 3:
                escalaMag = -2;
                break;
            case 4:
                escalaMag = -3;
                break;
            case 5:
                escalaMag = -6;
                break;
        }
    }

    float pot(int cuantos){
        float reg = 1f;
        for(int i = 1; i <= cuantos; i ++){
            reg *= 10f;
        }
        return reg;
    }

    /**
    * @param args the command line arguments
    */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgre;
    private javax.swing.JComboBox cmbColor;
    private javax.swing.JComboBox cmbEscalaMag;
    private javax.swing.JComboBox cmbEscalaX;
    private javax.swing.JComboBox cmbEscalaY;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txtMag;
    private javax.swing.JTextField txtX;
    private javax.swing.JTextField txtY;
    // End of variables declaration//GEN-END:variables

}
